package com.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.entity.pojo.WrTeamUser;
import com.mapper.WrTeamUserMapper;
import com.service.TeamUserService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
public class TeamUserServiceImpl implements TeamUserService {

    @Resource
    private WrTeamUserMapper wrTeamUserMapper;

    @Override
    public int addUserToTeam(int UserId, int TeamId) {
        WrTeamUser teamUser = wrTeamUserMapper.selectOne(new QueryWrapper<WrTeamUser>().eq("user_id", UserId)
                .eq("team_id", TeamId).eq("deleted", 0));
        if (teamUser == null) {
            return wrTeamUserMapper.insert(new WrTeamUser().setTeamId(TeamId).setUserId(UserId));
        } else {
            return 1;
        }
    }

    @Override
    public int removeUserFromTeam(int UserId, int TeamId) {
        WrTeamUser teamUser = wrTeamUserMapper.selectOne(new QueryWrapper<WrTeamUser>().eq("user_id", UserId).eq("team_id", TeamId));
        return wrTeamUserMapper.updateById(teamUser.setDeleted(1));
    }

    /**
     * 清除团队内的所有用户
     *
     * @param TeamId
     * @return
     */
    @Override
    public int cleanUserTeam(int TeamId) {
        return wrTeamUserMapper.cleanTeamAllUser(TeamId);
    }
}
